package com.yycx.module.bus.provider.handler.haiying.custombalance;

import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustomBalance;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 预付款收款
 *
 * @author zyf
 */
@Component
public class HyYfkTab1PageListHandler implements PageInterceptor<BusCustomBalance> {


    @Resource
    private FlowableHelp flowableHelp;

    @Resource
    private BusPaymentService paymentService;

    @Override
    public void prepare(CriteriaQuery<BusCustomBalance> cq, PageParams pageParams, EntityMap params) {
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = params.getLong("companyId", openUser.getCompanyId());
        cq.select(BusCustomBalance.class, "*");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.eq(true, "optType", CommonConstants.INT_1);
        cq.eq(true, "bcb.companyId", companyId);
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "amountDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "amountDate", endDate);
        cq.orderByDesc("bcb.createTime");
    }


    @Override
    public List<EntityMap> complete(CriteriaQuery<BusCustomBalance> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            //认款ID
            Long rkId = entityMap.getLong("rkId");
            if (FlymeUtils.isNotEmpty(rkId)) {
                BusPayment payment = paymentService.getById(rkId);
                if(FlymeUtils.isNotEmpty(payment)) {
                    //认款状态
                    entityMap.put("paymentState", payment.getPaymentState());
                    entityMap.put("paymentNo", payment.getPaymentNo());
                    String processInstanceId = payment.getProcessInstanceId();
                    if (FlymeUtils.isNotEmpty(processInstanceId)) {
                        EntityMap taskInfo = flowableHelp.getTaskInfo(processInstanceId);
                        entityMap.putAll(taskInfo);
                        entityMap.put("processInstanceId", processInstanceId);
                    }
                }
            }
            //退款ID
            Long refundId = entityMap.getLong("refundId");
            if (FlymeUtils.isNotEmpty(refundId)) {
                BusPayment refundPayment = paymentService.getById(refundId);
                //退款状态
                entityMap.put("refundPaymentState", refundPayment.getPaymentState());
                entityMap.put("refundPaymentDate", DateUtils.formatDate(refundPayment.getPayDate()));
            }
        }
        return result;
    }

}
